﻿using OrderSystem.DataAccess;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Windows.Forms;


namespace OrderSystem
{
    public partial class FormAddProductType : FormTemplet
    {
        public FormAddProductType()
        {
            this.InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.txtProductName.Text = CommonMethod.Fun1(this.txtProductName.Text);
            this.txtThick.Text = CommonMethod.Fun1(this.txtThick.Text);
            this.txtUnit.Text = CommonMethod.Fun1(this.txtUnit.Text);
            this.txtDefaultPrice.Text = CommonMethod.Fun1(this.txtDefaultPrice.Text);
            this.txtLabelMultiple.Text = CommonMethod.Fun1(this.txtLabelMultiple.Text);
            this.txtRemark.Text = CommonMethod.Fun1(this.txtRemark.Text);
            this.txtLimitedQuantity.Text = CommonMethod.Fun1(this.txtLimitedQuantity.Text);
            this.txtLimitedArea.Text = CommonMethod.Fun1(this.txtLimitedArea.Text);
            if (this.txtProductName.Text.Trim() == "" || this.txtProductName.Text.Trim().Length > 64)
            {
                MessageBox.Show("产品类型的 长度 应该在 1-64 个字符之间 ！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtProductName.Focus();
            }
            else if (this.txtThick.Text.Trim() == "" || this.txtThick.Text.Trim().Length > 16)
            {
                MessageBox.Show("产品类型的 厚度 应在 1-16 个字符之间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtThick.Focus();
            }
            else if (this.txtUnit.Text.Trim() == "" || this.txtUnit.Text.Trim().Length > 8)
            {
                MessageBox.Show("产品类型的 单位 应在 1-8 个字符之间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtUnit.Focus();
            }
            else if (!Regex.IsMatch(this.txtDefaultPrice.Text.Trim(), "^(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$") || Convert.ToDecimal(this.txtDefaultPrice.Text.Trim()) < 0m)
            {
                MessageBox.Show("价格 格式不正确，此处应该为不为负的数字，如有小数，最大为2位小数！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtDefaultPrice.Focus();
            }
            else if (!Regex.IsMatch(this.txtLabelMultiple.Text, "^[0-9]{1,}$") || Convert.ToInt32(this.txtLabelMultiple.Text.Trim()) <= 0)
            {
                MessageBox.Show("标签倍数 格式不正确，此处应该为大于零的正整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtLabelMultiple.Focus();
            }
            else if (this.txtRemark.Text.Trim().Length > 64)
            {
                MessageBox.Show("产品类型的 备注 应在 64 个字符以内", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtRemark.Focus();
            }
            else if (!Regex.IsMatch(this.txtLimitedQuantity.Text, "^[0-9]{1,}$") || Convert.ToInt32(this.txtLimitedQuantity.Text.Trim()) < 1)
            {
                MessageBox.Show("流程卡数量上限 格式不正确，此处应该为大于 1 的整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtLimitedQuantity.Focus();
            }
            else if (!Regex.IsMatch(this.txtLimitedArea.Text.Trim(), "^(([0-9]{1,8})|([0-9]{1,8}\\.[0-9]{1,2}))$") || Convert.ToDecimal(this.txtLimitedArea.Text.Trim()) <= 0m)
            {
                MessageBox.Show("流程卡面积上限 格式不正确，此处应该为大于 0 的正整数，如有小数，最大为 2 位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtLimitedArea.Focus();
            }
            else
            {
                ProductTypes productTypes = new ProductTypes();
                bool flag = false;
                try
                {
                    flag = productTypes.InsertProductType(this.txtProductName.Text.Trim(), this.txtThick.Text.Trim(), this.txtUnit.Text.Trim(), Convert.ToDecimal(this.txtDefaultPrice.Text.Trim()), Convert.ToInt32(this.txtLabelMultiple.Text.Trim()), this.txtRemark.Text.Trim(), Convert.ToInt32(this.txtLimitedQuantity.Text.Trim()), Convert.ToDecimal(this.txtLimitedArea.Text.Trim()));
                }
                catch (SqlException ex)
                {
                    int number = ex.Number;
                    if (number != 2627)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else
                    {
                        MessageBox.Show("这个产品类型已经存在，不能添加相同的产品类型！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtProductName.Focus();
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                productTypes.Dispose();
                if (flag)
                {
                    MessageBox.Show("产品类型录入成功！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    if (MessageBox.Show("继续输入吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        this.txtProductName.Text = "";
                        this.txtThick.Text = "";
                        this.txtUnit.Text = "";
                        this.txtLabelMultiple.Text = "";
                        this.txtRemark.Text = "";
                    }
                    else
                    {
                        base.Close();
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
